﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NewsMine.DomainObjects;
using System.Text.RegularExpressions;

namespace NewsDiscoveryEngine.Cleansing
{
    public class TitleBasedCleaner : ICleanser
    {

        public List<HtmlLink> Cleanse(List<HtmlLink> htmlLinks)
        {
            List<HtmlLink> resultedHtmlLinks = new List<HtmlLink>();
            foreach (HtmlLink link in htmlLinks)
            {
                if (IsValidHtmlLink(link) == true)
                {
                    resultedHtmlLinks.Add(link);
                }
                else
                    continue;
            }

            return resultedHtmlLinks;
        }

        public bool IsValidHtmlLink(HtmlLink htmlLink)
        {
            //todo: Scenarios to clean the url.
            // #comments.... 

            string[] invalidTitlePatterns = GetInvalidTitlePatternsToBeRemoved();

            if (string.IsNullOrEmpty(htmlLink.Title) || string.IsNullOrWhiteSpace(htmlLink.Title))
                return false;


            foreach (var invalidTitlePattern in invalidTitlePatterns)
            {
                if (Regex.IsMatch(htmlLink.Title, invalidTitlePattern) == true)
                    return false;
                else
                    continue;
            }

            return true;
        }

        private string[] GetInvalidTitlePatternsToBeRemoved()
        {
            //todo: These patterns can be read from the file; 
            return new string[] { "^\\d$", "^1 comment$", "^\\d[s]+[(comments)]+$" };
        }
    }

}
